package com.ziyun.erp.modules.material.dao;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.ziyun.erp.modules.material.entity.ErpStockEntity;
import com.ziyun.erp.modules.material.entity.storeStockMaterialEntity;
import com.ziyun.erp.modules.supp.entity.SellerInfoEntity;
import io.renren.common.utils.R;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Author ZhaoWei
 * @Date 2018/11/08 09:03
 * @Version V1.0 com.ziyun.erp.modules.material.dao.StoreStockMaterialDao
 */
public interface StoreStockMaterialDao extends BaseMapper<storeStockMaterialEntity> {

	@Insert("insert into store_stock_material (stock_id,material_id,material_quantity,cost,money,unit,remark) values (#{stockId},#{materialId},#{materialQuantity},#{cost},#{money},#{unit},#{remark}) ")
	int insertStoreStockMaterial(@Param("stockId") Integer stockId,
							 @Param("materialId") String materialId,
							 @Param("materialQuantity") BigDecimal materialQuantity,
							 @Param("cost") BigDecimal cost,
							 @Param("money") BigDecimal money,
							 @Param("unit") String unit,
							 @Param("remark") String remark
							 );


	@Select("select * from store_stock_material where material_id = #{materialId}")
	List<storeStockMaterialEntity> selectCostAndStockIdByMaterialId(Integer materialId);

	@Select("select * from erp_stock where stock_id = #{StockId}")
	ErpStockEntity getErpStockEntity(Integer StockId);


	@Select("select * from seller_info where seller_id = #{institution}")
	SellerInfoEntity getSellerInfoEntity(String institution);


	@Select("select a.seller_id as sellerId,a.seller_name as sellerName from seller_info a where a.self = 1 and seller_id not in (1,18,20)")
	List<SellerInfoEntity> getMDList();


	@Select("select stock_id from store_stock_material where stock_id = #{ErpstockId} and material_id = #{materialId}")
	Integer getStockId(@Param("ErpstockId") Integer ErpstockId,@Param("materialId") String materialId);


	@Update("update store_stock_material set cost = #{cost} where stock_id = #{storeStockMaterialStockId} and material_id = #{materialId}")
	int updateCost(@Param("materialId") String materialId, @Param("storeStockMaterialStockId") Integer storeStockMaterialStockId, @Param("cost") BigDecimal cost);


	@Insert(" insert into store_stock_material (material_id,stock_id,cost) values (#{materialId,jdbcType=VARCHAR},#{erpstockId},#{cost})")
	int insertCost(@Param("materialId") String materialId, @Param("erpstockId") Integer erpstockId, @Param("cost") BigDecimal cost);

	@Update("update store_stock_material set material_quantity = material_quantity - #{tempResult} where stock_material_id = #{stockMaterialId}")
    void checkOutUpdate(@Param("stockMaterialId") Integer stockMaterialId,@Param("tempResult") BigDecimal tempResult);

	@Update("update store_stock_material set material_quantity = material_quantity + #{checkResult} where material_id = #{materialId} and stock_id = #{stockId} and del = 0")
	Integer checkInUpdate(@Param("stockId")Integer stockId, @Param("materialId")String materialId, @Param("checkResult")BigDecimal checkResult);
}
